<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <script src="static/js/jquery-2.1.1.min.js"></script>
    <%@include file="layUI.jsp"%>
</head>
<body>
<%--action="/doGoodsServlet?action=addGoods"--%>
<form method="post" enctype="multipart/form-data" id="forms"  action="/doGoodsServlet?action=addGoods">
    <di id="d1"></di>
    商品名称<input type="text"  name="goodsName" id="goodsName">
    小分类名称<select name="goodsSmalId" id="goodsSmalId">
    </select>
    商品的价格<input type="text"  name="goodsMoney" id="goodsMoney">
    商品的数量<input type="text" name="goodsNumber" id="goodsNumber" >
    商品的图像<input type="file"  name="goodsImage" id="goodsImage"><br>
    商品的运费<input type="text"  name="goodsCarriage" id="goodsCarriage">
    商品的类型<select name="goodsType" id="goodsType">
    <option value="0">新品</option>
    <option value="1">二手</option>
</select>
    商品的折扣<select name="goodsDiscId" id="goodsDiscId">
</select>
    <input type="button" value="添加" class="layui-btn" id="sub" onclick="Verification()">
</form>
</body>

<script type="text/javascript">
    $(function () {
        var flag = '${param.flag}'
        if(flag=="false"){
            alert("添加失败！")
        }
        addSmall();
        addDiscount();
    })
    //添加小分类的下拉列表
    function addSmall() {
        $.getJSON("/SmallServlet", "action=select", callback);
        function callback(data) {
            for (var i = 0; i <data.length ; i++) {
                $("[id=goodsSmalId]").append("<option value='" + data[i].smallId + "'>" + data[i].smallName + "</option>");
            }
        }
    }

    //添加折扣率的下拉列表
    function addDiscount() {
        $.getJSON("/SmallServlet", "action=select2", callback);
        function callback(data) {
            $(data).each(function () {
                $("[id=goodsDiscId]").append("<option value='" + this.id + "'>" + this.discRate + "</option>");
            })
        }
    }

    //验证是否添加成功
    function Verification() {
        var flag=true;
        var goodsName=$("#goodsName").val();
        var goodsSmalId=$("#goodsSmalId").val();
        var goodsNumber=$("#goodsNumber").val();
        var goodsImage=$("#goodsImage").val();
        var goodsCarriage=$("#goodsCarriage").val();
        var goodsType=$("#goodsType").val();
        var goodsDiscId=$("#goodsDiscId").val();
        if (goodsName==null ||  goodsName==""){
            alert("商品内容不能为空");
            return flag;
        }
        if (goodsSmalId==null || goodsSmalId==""){
            alert("小分类内容不能为空");
            return flag;
        }
        if (goodsNumber==null || goodsNumber==""){
            alert("数量内容不能为空");
            return flag;
        }
        if (goodsImage==null || goodsImage==""){
            alert("图像内容不能为空");
            return flag;
        }
        if (goodsCarriage==null || goodsCarriage==""){
            alert("运费内容不能为空");
            return flag;
        }
        if (goodsType==null || goodsType==""){
            alert("商品类型内容不能为空");
            return flag;
        }
        if (goodsDiscId==null || goodsDiscId==""){
            alert("折扣内容不能为空");
            return flag;
        }
        if(goodsName!=null || goodsName!="" && goodsSmalId!=null || goodsSmalId!=""
        && goodsNumber!=null || goodsNumber!="" && goodsImage!=null || goodsImage!=""
        && goodsCarriage!=null || goodsCarriage!="" && goodsType!=null || goodsType!=""
        && goodsDiscId!=null || goodsDiscId!=""){
            $("#forms").submit();
        }
        return true;
    }

</script>
</html>
